/*
 * @Copyright: 2025 Copyright (C) AMCAX, All rights reserved.
 * @Author: Zhang
 * @Date: 2025/7/24 19:39
 * @LastEditTime: 2025/7/24 19:39
 * @LastEditors: Zhang
 * @Description:
 */
import { TopoDialogProps, NMMeshBaseCommand } from '../type';

import { CmdDialogForm } from 'nextcae-base';
import { useEffect, useRef, useState } from 'react';
import './index.scss';
import { schema, uiSchema } from './data';
import { widgetManager } from '@/common/managers';
import CmdInteractor from './CmdInteractor';
import { featDetect } from '@/domain';

const CurveLineDialog = ({ title, onClose }: TopoDialogProps) => {
  const [formData, setFormData] = useState({
    Options: {
      typeOptions: 'Feature',
    },
    Selection: {
      source: 'linePicking',
      TopLine: [],
      BottomLine: [],
    },
  });
  const [isFormDataValid, setIsFormDataValid] = useState(false); // 确定按钮是否可操作
  const cmdRef = useRef<CmdInteractor>(null); // 拾取组件

  useEffect(() => {
    cmdRef.current = new CmdInteractor(setIsFormDataValid);
    return () => {
      cmdRef.current.destroy();
      cmdRef.current = null;
    };
  }, []);

  useEffect(() => {
    widgetManager.hint('进入曲线识别命令!');
    return () => {
      widgetManager.hint();
    };
  }, []);

  // 提交
  const onSubmit = async () => {
    const getDataInfo = cmdRef.current.getDataInfo();
    const params = {
      type: formData.Options.typeOptions,
      root: getDataInfo.dxid as string,
      fidx: getDataInfo.fidx,
      points: getDataInfo.point.flat(),
    };
    await featDetect.curveDetect(params);
    onClose();
  };
  return (
    <>
      <CmdDialogForm
        title={title}
        titleIcon={'commands/featureInfo/curve-detect'}
        schema={schema}
        extraUiSchema={uiSchema}
        formData={formData}
        fields={{}}
        onChange={(data) => {
          setFormData(data.formData);
        }}
        onSubmit={onSubmit}
        onClose={onClose}
        isFormDataValid={true}
        width='300px'
        formClassName='structure-form'
      />
    </>
  );
};
export const cmdCurveLine: NMMeshBaseCommand = {
  id: 'cmdCurveLine',
  icon: 'commands/featureInfo/curve-detect',
  label: '曲线识别',
  create: (label: string, onClose: () => void, props) => (
    <CurveLineDialog
      title={label}
      onClose={onClose}
      props={props}
      placement='topLeft'
    />
  ),
};
